define(function(require, exports, module) {
	
	//lib工具包
	require('resources/js/avalon-2.0.js');
	//组件
	var schemaOperateTpl=require('text!resources/template/resInfo/resInfo_schema_operate_tpl.html');
		
	var ResInfoBaseEditor=require('resources/template/resInfo/resInfo_base_editor.js');
	var resInfoBaseEditorTpl=require('text!resources/template/resInfo/resInfo_base_editor_tpl.html');
	
	var curEntryId=-1;
	
	/**schemaOperate的容器，数据，父组件*/
	var ResInfoSchemaOperate=function(data,$container,config){
		//设值数据
		this.$container=$container
		this.resInfo=data;
		
		//初始化配置
		this.selfDelete=config.selfDelete===undefined?true:config.selfDelete;
		this.step=config.step||'draft';
		this.baseUrl=config.baseUrl;
		this.readOnly=config.readOnly===undefined?false:config.readOnly;
		
		//拼模版
		this.$div=$(schemaOperateTpl).prependTo(this.$container);
		this.$resInfoBaseDiv=$('.base-property',this.$div);
		$(resInfoBaseEditorTpl).prependTo(this.$resInfoBaseDiv);
		
		this.init();
	};
	
	ResInfoSchemaOperate.prototype={
		getCmpConfig:function(){
			return {
				step:this.step,
				baseUrl:this.baseUrl,
				readOnly:this.readOnly
			};
		},
		init:function(){
			this.render();
			this.bind();
			this.resInfoBaseEditor=new ResInfoBaseEditor(this.resInfo,this.$resInfoBaseDiv,this.getCmpConfig());
			
			this.sync();
			this.readOnlyRender();
		},
		readOnlyRender:function(){
			if(!this.readOnly)
				return ;
			
			$('input,textarea',this.$div).attr('readonly','readonly');
			$('select',this.$div).attr('disabled','disabled');
		},
		render:function(){
			
		},
		bind:function(){
			
		},
		sync:function(){
			var that=this,$vmId='resInfo-schema-'+this.resInfo.resId;
			this.$div.attr("ms-controller",$vmId);
			//使用avalon渲染
			this.vm = avalon.define({
                $id: $vmId,
                resInfo:this.resInfo,
                selfDelete:this.selfDelete,
                readOnly:this.readOnly,
                step:this.step,
                addEntry:function(){
                	that.vm.resInfo.schema.entries.unshift({
                		entryId:curEntryId--,
                		alias:'',
                		entryMemo:''
                	});
                },
                removeEntry:function(index){
                	that.vm.resInfo.schema.entries.removeAt(index);
                }
            });//联合资源的vm
			avalon.scan(this.$div.get(0),this.vm);
			//组件
			this.resInfoBaseEditor.fillData(this.vm.resInfo);
		},
		getData:function(){
			//获取数据，并校验
			this.resInfoError=this.resInfoBaseEditor.getError();//这里要放在提取this.resInfo之前
			this.resInfo=this.vm.resInfo.$model;
			var entryErrorFlag=false,entryDuplicate={},entryDuplicateFlag=false;;
			$.each(this.resInfo.schema.entries,function(index,entry){
				if(!entry.alias)
					entryErrorFlag=true;
				else if(!entryDuplicate[entry.alias])
					entryDuplicate[entry.alias]=1;
				else
					entryDuplicateFlag=true;
			});
			entryErrorFlag&&this.resInfoError.push('存在属性字段中文名为空');
			entryDuplicateFlag&&this.resInfoError.push('存在重名的属性字段');
			!this.resInfo.schema.entries.length&&this.resInfoError.push('至少添加一个属性字段');
		},
		destroy:function(){
			this.$div.remove();
		}
	};

	return ResInfoSchemaOperate;
});
